package com.k.lib_base

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 * BaseActivity
 */
abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity() {
    var mBinding: T? = null
        private set


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge() // 全屏
        mBinding = getBinding()

        mBinding?.let {
            ViewCompat.setOnApplyWindowInsetsListener(it.root) { v, insets ->
                val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
                insets
            }
        }

        mBinding?.lifecycleOwner = this // 将 lifecycleOwner 设置为当前 Activity

        initView()
    }

    abstract fun layoutId(): Int

    abstract fun initView()

    private fun getBinding(): T? {
        return DataBindingUtil.setContentView(this, layoutId())
    }

    override fun onDestroy() {
        super.onDestroy()

        mBinding = null
    }
}